<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../plugin/layui-v2.2.5/layui/css/layui.css">
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
  <form class="layui-form" method="post">
    <div class="layui-row">
      <div class="layui-col-md6">
        <div class="layui-form-item">
          <label class="layui-form-label">课程名</label>
          <div class="layui-input-block">
            <select name="courseid" lay-verify="required" lay-search id="addNew_course">
            	<option value=""></option>
            </select>
          </div>
        </div>
      </div>
      <div class="layui-col-md6">
        <div class="layui-form-item">
          <label class="layui-form-label">上课教师</label>
          <div class="layui-input-block">
            <select name="teacherName" id="addNew_teacher" lay-verify="required" lay-search lay-filter="addNew">
            	<option value=""></option>
            </select>
          </div>
        </div>
      </div>
    </div>
    <div class="layui-row">
      <div class="layui-col-md6">
        <div class="layui-form-item">
          <label class="layui-form-label">教师工号</label>
          <div class="layui-input-block">
            <input name="teacherid" id="addNew_workId" class="layui-input" type="text" autocomplete="off" lay-verify="title" disabled="disabled" value="">  
          </div>
        </div>
      </div>
      <div class="layui-col-md6">
        <div class="layui-form-item">
          <label class="layui-form-label">授课班级</label>
          <div class="layui-input-block">
            <select name="classid" lay-verify="required" lay-search id="addNew_class">
            	<option value=""></option>
            </select>
          </div>
        </div>
      </div>
    </div>
    <div class="layui-row">
      <div class="layui-col-md6">
        <div class="layui-form-item">
          <label class="layui-form-label">课程材料类型</label>
          <div class="layui-input-block">
            <select name="documenttype" lay-verify="required" lay-search>
              <option value=""></option>
              <option value="试卷">试卷</option>
              <option value="材料">课程设计材料</option>
              <option value="综合性材料">综合性设计性材料</option>
            </select>
          </div>
        </div>
      </div>
      <div class="layui-col-md6">
        <div class="layui-form-item">
          <label class="layui-form-label">授课学期</label>
          <div class="layui-input-block">
            <select id="period" lay-verify="required" lay-search name="period">
              <option value=""></option>
            </select>
          </div>
        </div>
      </div>
    </div>
    <div class="layui-row">
      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
          <textarea name="remark" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="formDemo" type="button">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary" id="resetBtn">重置</button>
      </div>
    </div>
  </form>
 
<script src="../plugin/layui-v2.2.5/layui/layui.js"></script>
<script src="../plugin/jquery-3.1.1.min.js"></script>
<script>
layui.use(['form','layer'], function(){
  var form = layui.form;
  
  //加载教师数据
  $.ajax({ 
	url: "/CMA/teacher/getAll",
	contentType: "application/x-www-form-urlencoded",
	type:"post",
	dataType : "json",
	success: function(data){
		//console.log(data.data.length);
		var optionData = data.data;
		var optionText = "";
		for(var i = 0; i < optionData.length; i++){
			optionText = optionText + "<option value="+optionData[i].workid+">"+optionData[i].name+"</option>";
		}
		//console.log("ss"+optionText);
		$("#addNew_teacher").append(optionText); 
		form.render('select');
      }
  });
  
  //加载课程数据
  $.ajax({ 
		url: "/CMA/course/getAllCourse",
		contentType: "application/x-www-form-urlencoded",
		type:"post",
		dataType : "json",
		success: function(data){
			//console.log(data.data);
			var optionData = data.data;
			var optionText = "";
			for(var i = 0; i < optionData.length; i++){
				optionText = optionText + "<option value="+optionData[i].courseid+">"+optionData[i].coursename+"</option>";
			}
			//console.log("ss"+optionText);
			$("#addNew_course").append(optionText); 
			form.render('select'); 
	      }
  });
  //加载班级数据
  $.ajax({ 
		url: "/CMA/classGroup/getAllClass",
		contentType: "application/x-www-form-urlencoded",
		type:"post",
		dataType : "json",
		success: function(data){
			//console.log(data.data);
			var optionData = data.data;
			var optionText = "";
			for(var i = 0; i < optionData.length; i++){
				optionText = optionText + "<option value="+optionData[i].id+">"+optionData[i].name+"</option>";
			}
			//console.log("ss"+optionText);
			$("#addNew_class").append(optionText); 
			form.render('select');  
	      }
  });
  
  //监听select
  form.on('select(addNew)', function(data){
    //console.log(data.elem); //得到select原始DOM对象
    //console.log(data.value); //得到被选中的值
    //console.log(data.othis); //得到美化后的DOM对象
    $("#addNew_workId").val(data.value);
  });   
  
  //监听提交
  form.on('submit(formDemo)', function(data){
    //console.log(JSON.stringify(data.field));
    console.log(data.field);
    $.ajax({ 
    	url: "/CMA/courseDocument/addNewDocument",
    	contentType: "application/x-www-form-urlencoded",
    	type:"post",
    	dataType : "json",
    	data : data.field,
    	success: function(data){
    		if(data.msg=="success"){
    			var alertMsg = layer.alert('插入成功', {
			      skin: 'layui-layer-molv' //样式类名  自定义样式
			      ,closeBtn: 1    // 是否显示关闭按钮
			      ,anim: 1 //动画类型
			      ,btn: ['继续插入','关闭窗口'] //按钮
			      ,icon: 6    // icon
			      ,yes:function(){
			    	  $("#resetBtn").click();
			    	  layer.close(alertMsg);
			    	  
			      }
			     ,btn2:function(){ 
			    	 var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		    		 parent.layer.close(index);
		         }
			   });
    			
    			/* layer.msg('插入成功', {
    				    time: 2000, //2s后自动关闭
    			}) */
    			/* var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    			parent.layer.close(index); */
    		}
    	}
      });
    return false;
  });
  
  //生成学期
  //获取当前年份
  var date=new Date;
  var year=date.getFullYear(); 
  var periodText = "";
  for(var i = 0; i < 3; i++){
	  for(var j = 0; j < 2; j++){
		  if(j == 0){
			  periodText = periodText + "<option value='"+year +"-"+ (year+1) +"第一学期'>"+year +"-"+ (year+1) +"第一学期"+"</option>";
		  }else{
			  periodText = periodText + "<option value='"+year +"-"+ (year+1) +"第二学期'>"+year +"-"+ (year+1) +"第二学期"+"</option>";
		  }
	  }
	  year--;
  }
  //console.log(periodText);
  $("#period").append(periodText);
  
});
</script>

</body>
</html>